﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using AnimalCare.Model;
using AnimalCare.Controller;
using AnimalCare.Service;

namespace AnimalCare.View
{
    public partial class FormCustomer : DevExpress.XtraEditors.XtraForm
    {
        public FormCustomer()
        {
            InitializeComponent();
            refreshGrid();
            InitGrid();
        }

        void InitGrid()
        {
        }
        protected void refreshGrid()
        {
            AnimalCareDBEntities entities;
            entities = new AnimalCareDBEntities();
            gridCustomer.DataSource = entities.Customers;
            gridCustomer.Refresh();
        }

        private void InsertSupplier_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(textNameCustomer.Text))
            {
                CustomerService.Insert(textNameCustomer.Text, textphoneCustomer.Text, textAddressCustomer.Text , textInfoCustomer.Text);
                refreshGrid();
                textNameCustomer.Text = "";
                textphoneCustomer.Text = "";
                textAddressCustomer.Text = "";
                textInfoCustomer.Text = "";
            }
            else
            {
                ValidateService.validateEmpty(textNameCustomer, errorProvider1, "Chưa nhập tên khách hàng");
            }
        }

        private void DeleteMenuItem_Click(object sender, EventArgs e)
        {
            int[] selectedRowsIndex = gridViewCustomer.GetSelectedRows();
            if (selectedRowsIndex.Length > 0)
            {
                int indexRow = selectedRowsIndex[0];
                Customer cl = (Customer)gridViewCustomer.GetRow(indexRow);
                CustomerService.Delete(cl.customer_id);
                refreshGrid();
            }
        }

        private void UpdateMenuItem_Click(object sender, EventArgs e)
        {
            int[] selectedRowsIndex = gridViewCustomer.GetSelectedRows();
            if (selectedRowsIndex.Length > 0)
            {
                int indexRow = selectedRowsIndex[0];
                Customer cl = (Customer)gridViewCustomer.GetRow(indexRow);
                CustomerService.Update(cl);
                refreshGrid();
            }
        }

        private void textNameCustomer_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(textNameCustomer.Text))
            {
                InsertSupplier.Enabled = true;
            }
            else
            {
                ValidateService.validateEmpty(textNameCustomer, errorProvider1, "Chưa nhập tên khách hàng");
                InsertSupplier.Enabled = false;
            }
        }
    }
}
